<template>
  <div class="home page-component">
    <el-carousel indicator-position="outside">
      <el-carousel-item v-for="item in 2" :key="item">
        <img src="~assets/images/web-banner1.png" alt="">
      </el-carousel-item>
    </el-carousel>
    <div class="search-container">
      <div class="search-wrapper">
        <div class="hospital-search">
          <el-autocomplete
            class="search-input"
            prefix-icon="el-icon-search"
            v-model="hosname"
            :fetch-suggestions="querySearchAsync"
            placeholder="点击输入医院名称"
            @select="handleSelect">
            <span slot="suffix" class="search-btn v-link highlight clickable selected">搜索 </span>
          </el-autocomplete>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="left">
        <div class="home-filter-wrapper">
          <div class="title"> 医院</div>
          <div>
            <div class="filter-wrapper">
        <span
          class="label">等级：</span>
              <div class="condition-wrapper">
          <span class="item v-link clickable"
                :class="hostypeActiveIndex === index ? 'selected' : ''"
                v-for="(item,index) in hostypeList" :key="item.id"
                @click="hostypeSelect(item.value, index)">{{ item.name }}</span>
              </div>
            </div>
            <div class="filter-wrapper">
      <span
        class="label">地区：</span>
              <div class="condition-wrapper">
        <span class="item v-link clickable"
              :class="provinceActiveIndex === index ? 'selected' : ''"
              v-for="(item,index) in districtList" :key="item.id"
              @click="districtSelect(item.value, index)">{{ item.name }}</span>
              </div>
            </div>
          </div>
        </div>
        <!-- 第一行-->
        <div class="v-scroll-list hospital-list">
          <!--1.郑州市中心医院-->
          <div class="v-card clickable list-item" v-for="item in list" :key="item.id" style="float: left">
            <div class="">
              <div class="hospital-list-item hos-item" index="0" @click="show(item.hoscode)">
                <div class="wrapper">
                  <div class="hospital-title"> {{ item.hosname }}</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>{{ item.param.hostype }}
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>每天{{ item.bookingRule.releaseTime }}放号
                    </div>
                  </div>
                </div>
                <img :src="'data:image/jpeg;base64,'+item.logoData"
                     :alt="item.hosname"
                     class="hospital-img">
              </div>
            </div>
          </div>
          <!--2.河南省人民医院-->
          <div class="v-card clickable list-item" style="float: right">
            <div class="">
              <div class="hospital-list-item hos-item">
                <div class="wrapper">
                  <div class="hospital-title">河南省人民医院</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>三级甲等
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>每天8:30放号
                    </div>
                  </div>
                </div>
                <img
                  src="https://hospital-yygh.oss-cn-beijing.aliyuncs.com/%E5%8C%BB%E9%99%A2Logo/%E6%B2%B3%E5%8D%97%E7%9C%81%E4%BA%BA%E6%B0%91%E5%8C%BB%E9%99%A2.png"
                  class="hospital-img">
              </div>
            </div>
          </div>
        </div>

        <!--第二行-->
        <div class="v-scroll-list hospital-list">
          <!--3.河南省中医院-->
          <div class="v-card clickable list-item" style="float: left">
            <div class="">
              <div class="hospital-list-item hos-item">
                <div class="wrapper">
                  <div class="hospital-title">河南省中医院</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>三级甲等
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>每天8:30放号
                    </div>
                  </div>
                </div>
                <img
                  src="https://hospital-yygh.oss-cn-beijing.aliyuncs.com/%E5%8C%BB%E9%99%A2Logo/%E6%B2%B3%E5%8D%97%E7%9C%81%E4%B8%AD%E5%8C%BB%E5%8C%BB.png"
                  class="hospital-img">
              </div>
            </div>
          </div>
          <!--4.惠济区人民医院-->
          <div class="v-card clickable list-item" style="float: right">
            <div class="">
              <div class="hospital-list-item hos-item">
                <div class="wrapper">
                  <div class="hospital-title">惠济区人民医院</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>二级甲等
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>每天8:30放号
                    </div>
                  </div>
                </div>
                <img
                  src="https://hospital-yygh.oss-cn-beijing.aliyuncs.com/%E5%8C%BB%E9%99%A2Logo/%E6%83%A0%E6%B5%8E%E5%8C%BA%E4%BA%BA%E6%B0%91%E5%8C%BB%E9%99%A2.png"
                  class="hospital-img">
              </div>
            </div>
          </div>
        </div>

        <!--第三行-->
        <div class="v-scroll-list hospital-list">
          <!--5.郑州大学第一附属医院-->
          <div class="v-card clickable list-item" style="float: left">
            <div class="">
              <div class="hospital-list-item hos-item">
                <div class="wrapper">
                  <div class="hospital-title">郑州大学第一附属医院</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>三级甲等
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>每天8:30放号
                    </div>
                  </div>
                </div>
                <img
                  src="https://hospital-yygh.oss-cn-beijing.aliyuncs.com/%E5%8C%BB%E9%99%A2Logo/%E9%83%91%E5%B7%9E%E5%A4%A7%E5%AD%A6%E7%AC%AC%E4%B8%80%E9%99%84%E5%B1%9E%E5%8C%BB%E9%99%A2.png"
                  class="hospital-img">
              </div>
            </div>
          </div>
          <!--6.郑州市金水区总医院-->
          <div class="v-card clickable list-item" style="float: right">
            <div class="">
              <div class="hospital-list-item hos-item">
                <div class="wrapper">
                  <div class="hospital-title">郑州市金水区总医院</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>二级甲等
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>每天8:30放号
                    </div>
                  </div>
                </div>
                <img
                  src="https://hospital-yygh.oss-cn-beijing.aliyuncs.com/%E5%8C%BB%E9%99%A2Logo/%E9%83%91%E5%B7%9E%E5%B8%82%E9%87%91%E6%B0%B4%E5%8C%BA%E6%80%BB%E5%8C%BB%E9%99%A2.png"
                  class="hospital-img">
              </div>
            </div>
          </div>
        </div>

      </div>
      <div class="right">
        <div class="common-dept">
          <div class="header-wrapper">
            <div class="title"> 常见科室</div>
            <div class="all-wrapper"><span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <span class="item v-link clickable dark">神经内科 </span>
            <span class="item v-link clickable dark">消化内科 </span>
            <span class="item v-link clickable dark">呼吸内科 </span>
            <span class="item v-link clickable dark">内科 </span>
            <span class="item v-link clickable dark">神经外科 </span>
            <span class="item v-link clickable dark">妇科 </span>
            <span class="item v-link clickable dark"> 产科 </span>
            <span class="item v-link clickable dark">儿科 </span>
          </div>
        </div>
        <div class="space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper"><span
                class="iconfont title-icon"></span>
              </div>
              <span class="title">平台公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">关于延长郑州大学国际医院放假的通知 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">郑州市金水区总医院部分科室医生门诊医 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 惠济区人民医院号源暂停更新通知 </span>
            </div>
          </div>
        </div>
        <div class="suspend-notice-list space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper">
                <span class="iconfont title-icon"></span>
              </div>
              <span class="title">停诊公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 河南省中医院停诊公告 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 河南省人民医院诊停诊公告 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">郑州大学第一附属医院停诊公告 </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>

import hospApi from '@/api/hosp'
import dictApi from '@/api/dict'

export default {

  /**
   * 进入页面时,就获取医院列表
   * nuxt服务端渲染,异步显示医院列表,在访问页面时就最先执行执行,且只执行一次
   */
  asyncData({params, error}) {
    return hospApi.getPageList(1, 10, null)
      .then(response => {
        return {
          list: response.data.pages.content,
          pages: response.data.pages.totalPages
        }
      })
  },
  data() {
    return {
      searchObj: {},
      page: 1,
      limit: 10,

      hosname: '', //医院名称
      hostypeList: [], //医院等级集合
      districtList: [], //地区集合

      hostypeActiveIndex: 0,
      provinceActiveIndex: 0
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      /**
       * 查询医院等级列表
       */
      dictApi.findByDictCode('Hostype')
        .then(response => {
          this.hostypeList = []
          // 向hostypeList添加"全部"
          this.hostypeList.push({"name": "全部", "value": ""})
          // 把接口返回数据遍历添加到hostypeList
          for (let i = 0; i < response.data.list.length; i++) {
            this.hostypeList.push(response.data.list[i])
          }
        })

      /**
       * 查询地区数据
       */
      dictApi.findByDictCode('Zhengzhou')
        .then(response => {
          this.districtList = []
          this.districtList.push({"name": "全部", "value": ""})
          for (let i in response.data.list) {
            this.districtList.push(response.data.list[i])
          }
        })
    },

    // 查询医院列表
    getList() {
      hospApi.getPageList(this.page, this.limit, this.searchObj)
        .then(response => {
          for (let i in response.data.pages.content) {
            this.list.push(response.data.pages.content[i])
          }
          this.page = response.data.pages.totalPages
        })
    },

    //根据医院等级查询
    hostypeSelect(hostype, index) {
      //准备数据
      this.list = []
      this.page = 1
      this.hostypeActiveIndex = index
      this.searchObj.hostype = hostype
      //调用查询医院列表方法
      this.getList()
    },

    //根据地区查询医院
    districtSelect(districtCode, index) {
      this.list = []
      this.page = 1
      this.provinceActiveIndex = index
      this.searchObj.districtCode = districtCode
      this.getList();
    },

    /**
     * 在输入框输入值，弹出下拉框，显示相关内容
     * 参数1:输入的内容
     * 参数2:callback回调
     */
    querySearchAsync(queryString, cb) {
      this.searchObj = []
      if (queryString == '') return
      hospApi.getByHosname(queryString).then(response => {
        for (let i = 0, len = response.data.list.length; i < len; i++) {
          response.data.list[i].value = response.data.list[i].hosname
        }
        cb(response.data.list)
      })
    },

    // 在下拉框选择某一个内容，执行下面方法，跳转到详情页面中
    handleSelect(item) {
      window.location.href = '/hospital/' + item.hoscode
    },

    // 点击某个医院名称，跳转到详情页面中
    show(hoscode) {
      window.location.href = '/hospital/' + hoscode
    }
  }
}
</script>
